package com.arvato.minipro.service.mall;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.EcOrderCommodityDo;
import com.arvato.minipro.bean.bean_do.mall.EcOrderDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.EcCommodityVo;
import com.arvato.minipro.bean.bean_vo.mall.EcOrderCommodityVo;
import com.arvato.minipro.bean.bean_vo.mall.EcOrderVo;
import com.arvato.common.interceptor.ArvatoInterceptor;
import com.arvato.minipro.mapper.mall.EcOrderCommodityMapper;
import com.arvato.minipro.mapper.mall.EcOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Describe：商品订单service
 * 
 * @author smallking
 *
 *  2018年2月8日
 */
@Service
@Transactional
public class OrderService {
	private static final Logger logger = LoggerFactory.getLogger(ArvatoInterceptor.class);
	
	@Autowired
	private EcOrderMapper orderMapper;
	
	@Autowired
	private EcOrderCommodityMapper orderCommodityMapper;
	
	@Autowired
	private CommodityService commodityService;
	
	/**
	 * 根据Query查询用户订单列表
	 * @param query
	 * @return
	 */
	public PageData listOrderByQuery(Query query) {
		// 设置分页参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 执行查询
		List<EcOrderVo> userList = orderMapper.listOrderByQuery(query);
		// 得到结果
		PageInfo<EcOrderVo> pageInfo = new PageInfo<EcOrderVo>(userList);
		List<EcOrderVo> list = pageInfo.getList();
		for(EcOrderVo order : list) {
			try {
				setOrderDetail(order);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("查询订单关联的商品的时候出错", e);
			}
		}
		PageData data = new PageData(pageInfo.getTotal(), list);
		return data;
	}

	/**
	 * 数据库新增订单记录
	 * @param orderDo
	 * @param commodityInfoStr
	 * @return
	 */
	public int insert(EcOrderDo orderDo, String commodityInfoStr) {
		int row = orderMapper.insert(orderDo);
		if(row == 1) {
			Query query = new Query();
			query.put("userId", orderDo.getBuyerId());
			String[] split = commodityInfoStr.split("&");
			for(String str : split) {
				String commodityId = str.split(",")[0];
				int count = Integer.valueOf(str.split(",")[1]);
				EcOrderCommodityDo orderCommodityDo = new EcOrderCommodityDo();
				orderCommodityDo.setCommodityId(commodityId);
				orderCommodityDo.setCount(count);
				orderCommodityDo.setOrderId(orderDo.getOrderId());
				orderCommodityMapper.insert(orderCommodityDo);
			}
		}
		return row;
	}
	
	/**
	 * 查询设置订单的商品列表
	 * @param order
	 */
	private void setOrderDetail(EcOrderVo order) {
		Query query = new Query();
		query.put("orderId", order.getOrderId());
		List<EcOrderCommodityVo> orderCommodityVoList = orderCommodityMapper.listByQuery(query);
		List<EcCommodityVo> commodityVoList = new ArrayList<EcCommodityVo>();
		for(EcOrderCommodityVo orderCommodityVo : orderCommodityVoList) {
			EcCommodityVo commodityVo = commodityService.getCommodityById(orderCommodityVo.getCommodityId());
			commodityVo.setCount(orderCommodityVo.getCount());
			commodityVoList.add(commodityVo);
		}
		order.setCommodityList(commodityVoList);
	}
	/**
	 * 更新订单
	 * @param order
	 * @return
	 */
	public int update(EcOrderDo order) {
		return orderMapper.update(order);
	}
	
}
